package com.helen.springcloud.lb;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author yanghl
 * @Description:
 * @date 2022/8/6 22:18
 */
//加入容器
@Component
@Slf4j
public class MyLB implements ILoadBalance{

    /**
     * 新建一个原子整形类
     */
    private AtomicInteger atomicInteger = new AtomicInteger(0);

    /**
     * 获取当前请求是第几次请求
     * @return
     */
    public final int getAndIncrement(){
        int current;
        int next;
        do {
            current = this.atomicInteger.get();
            // 如果current是最大值，重新计算，否则加1（防止越界）
            next = current >= Integer.MAX_VALUE ? 0 : current + 1;
            // 进行CAS判断，如果不为true，进行自旋
        }while (!this.atomicInteger.compareAndSet(current, next));
        log.info("****第几次访问，次数next：" + next);
        return next;
    }

    @Override
    public ServiceInstance instances(List<ServiceInstance> serviceInstance) {
        if (serviceInstance.size() <= 0){
            return null;
        }
        // 进行取余
        int index = getAndIncrement() % serviceInstance.size();
        // 返回选中的服务实例
        return serviceInstance.get(index);
    }
}
